<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-6-21
 * Func   菜单管理
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('link');
if(IS_POST){
    if($a == 'service_add'){
        $data['url'] = $_POST['sUrl'];
        $row = $db->affected_rows("select id from ".Table('weblist')." where tid = 1");
        if(!$row){            
            $data['tid'] = 1;
            $data['inputtime'] = time();
            $db->exec("insert into ".Table("weblist").CreateInsertSql($data));
            AjaxReturn(['error'=>'0','msg'=>'操作成功']);
        }else{
            $db->exec("update ".Table('weblist')." set ".CreateUpdateSql($data)." where tid = 1");
            AjaxReturn(['error'=>'0','msg'=>'操作成功']);
        }
    }else{
        $id = intval($_REQUEST['id']);
        $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];

        //  表单数据
        $post = $_POST['data'];

        $ins = [];
        $ins['title']    =   NoBadStr($post['title']);
        $ins['summary']       =   $post['summary'];
        $ins['url']      =   NoBadStr($post['url']);
        $ins['listsort']      =   NoBadStr($post['listsort']);

        if($data['id']){
            //修改
            $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
        }else{
            $ins['dateline']    =   time();
            $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
        }
        AjaxReturn(['error'=>'0','msg'=>'操作成功']);        
    }
}

if($a == 'add'){
    $id=NoBadStr($_REQUEST['id']);
    $data=$db->getrow("select * from ".$table." where id='{$id}'");
    $tpl->display($m.'/linkadd');
}elseif($a == 'del'){
    $id = NoBadStr($_REQUEST['id']);
    if(!$id){
        AjaxReturn(['error'=>'1','msg'=>'参数丢失']);
    }
    $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
    AjaxReturn(['error'=>'0','msg'=>'删除成功']);
}else{
    $service = $db->getrow('select url from '.Table('weblist').' where tid = 1');
    $data  = array();
    $where = ' FROM '.$table.' t WHERE 1 ';

    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    if($tmp['p']){
        $page = max(1,intval($_GET['page']));
        $size = 20;
        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

        $orderby = ' ORDER BY t.listsort ASC ';
        $url = '?m='.$m.'&c='.$c.'&a='.$a;
        $data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
        $multi = Multi($url,$page,$tmp['p'],$size);
    }
    $tpl->display($m.'/service');
}
?>